﻿//-----------------------------------------------------------------------
// <copyright file="ClientHome.cs" company="FCIS">
//     Company copyright tag.
// </copyright>
// <author>Ramy Medhat</author>
// <date>24/8/2011</date>
// <reviewer></reviewer>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FCISPlus.Client.Communicator;

namespace FCISPlus.Client.UI
{
    /// <summary>
    /// The Client Form.
    /// </summary>
    public partial class ClientHome : Form
    {
        /// <summary>
        /// The client communicator.
        /// </summary>
        private IFCISPlusClientCommunicator communicator;

        /// <summary>
        /// The current User username
        /// </summary>
        private string userName;
        

        /// <summary>
        /// Initializes a new instance of the <see cref="ClientHome"/> class.
        /// </summary>
        public ClientHome()
        {
            InitializeComponent();
            this.communicator = FCISPlusClientFactory.GetCommunicator();
        }

        /// <summary>
        /// Handles the Load event of the ClientHome control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ClientHome_Load(object sender, EventArgs e)
        {
            LoginWindow login = new LoginWindow();

            DialogResult dialog = login.ShowDialog();

            if (dialog == System.Windows.Forms.DialogResult.OK)
            {
                // Check login.
                bool result = this.communicator.Login(login.Username, login.Password);

                if (!result)
                {
                    MessageBox.Show("Failed to login");
                    this.Close();
                }
            }
            else if (dialog == System.Windows.Forms.DialogResult.Retry)
            {
                // Signup.
                bool result = this.communicator.Signup(login.Username, login.Password);

                if (!result)
                {
                    MessageBox.Show("Failed to signup");
                    this.Close();
                }
            }
            else 
            {
                // User canceled out of login window.
                MessageBox.Show("You must login first");
                this.Close();
            }

            userName = login.Username;
            //We have successfully logged in
            this.Text = userName+" Home";
        }

        private void tsbMessages_Click(object sender, EventArgs e)
        {
            Messages messagesForm = new Messages(communicator);
            messagesForm.Text = userName + " Messages";
            messagesForm.Show();
        }

        private void tsbFeed_Click(object sender, EventArgs e)
        {
            Feeds feedsForm = new Feeds(communicator);
            feedsForm.Text = userName + " Feeds";
            feedsForm.Show();
        }

        private void tsbAddPost_Click(object sender, EventArgs e)
        {
            NewPost newPostForm = new NewPost(communicator);            
            newPostForm.Show();
        }

        private void tsbFriends_Click(object sender, EventArgs e)
        {
            ManageFriends friendsForm = new ManageFriends(communicator);
            friendsForm.Show();
        }

        private void tsbLogOut_Click(object sender, EventArgs e)
        {
            communicator.Logout();
            this.Close();            
        }
    }
}
